package com.ksyun.seckill.service.impl.V7;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ksyun.seckill.constants.SecKillImplVer;
import com.ksyun.seckill.dto.OrderbyPhoneDto;
import com.ksyun.seckill.entity.PurchaseOrder;
import com.ksyun.seckill.mapper.PurchaseOrderMapper;
import com.ksyun.seckill.service.OrderService;
import com.ksyun.seckill.service.SkService;
import com.ksyun.seckill.util.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DefaultOrderServiceImplV7 extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements OrderService,SkService {
    /**
     * 检验秒杀活动的合法性，如活动是不存在，活动是否允许参与秒杀等.
     *
     * @param campaignId 活动id.
     */
    @Override
    public void validateCampaign(Long campaignId) {

    }


    /**
     * 指定活动，用户是否已下过单，同一个手机号，同一个活动，只能参与一次.
     *
     * @param campaignId 活动id.
     * @param phone      手机号.
     * @return
     */
    @Override
    public Boolean haveOrder(Long campaignId, String phone) {
        return null;
    }


    /**
     * 秒杀.
     *
     * @param campaignId 活动id.
     * @param phone      手机号.
     * @param path       秒杀URL路径.
     */
    @Override
    public void secKill(Long campaignId, String phone, String path) {

    }

    /**
     * 服务实现版本.
     *
     * @return
     */
    @Override
    public SecKillImplVer getSecKillImplVer() {
        return SecKillImplVer.V7;
    }

    /**
     * 生成秒杀URL路径.
     *
     * @param campaignId 活动id.
     * @param phone      手机号.
     * @param verifyCode 验证码
     * @return
     */
    @Override
    public String generateSecKillPath(Long campaignId, String phone, String verifyCode) {
        return SkService.super.generateSecKillPath(campaignId, phone, verifyCode);
    }

    /**
     * 检查秒杀URL路径合法性.
     *
     * @param campaignId 活动id.
     * @param phone      手机号.
     * @param path       秒杀URL路径.
     * @return
     */
    @Override
    public boolean checkSecKillPath(Long campaignId, String phone, String path) {
        return SkService.super.checkSecKillPath(campaignId, phone, path);
    }

    /**
     * 根据phone查询对应账单列表
     * @param phone
     * @return
     */
    @Override
    public List<OrderbyPhoneDto> listByUserPhone(String phone) {
        if(StringUtils.isEmpty(phone)||phone.trim().length()==0){
            throw new ServiceException("手机号为空!");
        }
        QueryWrapper<PurchaseOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("user_phone",phone);
        List<PurchaseOrder> orders = this.baseMapper.selectList(wrapper);
        if(orders.isEmpty()){
            return Collections.emptyList();
        }
        List<OrderbyPhoneDto> ret_list= orders.stream().map(item->{
            OrderbyPhoneDto target= orderEntityToDto(item);
            return target;
        }).collect(Collectors.toList());
        return ret_list;
    }

    /**
     * 将订单实体转化为dto返回。
     * @param item
     * @return
     */
    private OrderbyPhoneDto orderEntityToDto(PurchaseOrder item) {
        OrderbyPhoneDto target = new OrderbyPhoneDto();
        target.setUserPhone(item.getUserPhone());
        target.setCampaignName(item.getCampaignName());
        target.setCreateTime(item.getCreateTime());
        target.setExpirationTime(item.getExpirationTime());
        target.setFriendCode(item.getFriendCode());
        //本次实验暂时没看见已核销的状态情况
        if(LocalDateTime.now().isAfter(item.getExpirationTime())){
            target.setStatusName("已过期");
        }else{
            switch (item.getStatus()){
                case 1: target.setStatusName("未核销");break;
                case 2: target.setStatusName("作废");break;
                case 3: target.setStatusName("已核销");break;
                default: throw new ServiceException("订单状态未知异常!");
            }
        }
        return target;
    }
}
